package com.patrol.logisticsmanage.ui.orders;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.R2;
import com.patrol.logisticsmanage.common.LoadingProgressDialog;
import com.patrol.logisticsmanage.net.AppConfig;
import com.patrol.logisticsmanage.net.BaseObserver;
import com.patrol.logisticsmanage.net.BaseReponse;
import com.patrol.logisticsmanage.net.BaseRequest;
import com.patrol.logisticsmanage.ui.appear.model.ServiceOrder;
import com.patrol.logisticsmanage.ui.appear.model.SubCamp;
import com.patrol.logisticsmanage.ui.base.BaseActivity;
import com.patrol.logisticsmanage.ui.canteen.utils.GsonUtil;
import com.patrol.logisticsmanage.ui.canteen.utils.LogUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

@Route(path = "/app/myOrders")
public class MyOrdersActivity extends BaseActivity {

    @BindView(R.id.startdate)
    TextView startdate;
    @BindView((R.id.enddate))
    TextView enddate;
    @BindView(R.id.startquery)
    Button startquery;

    @BindView(R.id.rcy_my_orders)
    RecyclerView rcyMyOrders;

    OrdersAdapter ordersAdapter;
    List<OrderInfo> k = new ArrayList<>();
    LoadingProgressDialog loadingProgressDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_orders);
        ButterKnife.bind(this);

        rcyMyOrders.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));

        ordersAdapter = new OrdersAdapter(mContext);
        ordersAdapter.showEmptyView(true);

        ordersAdapter.setList(k);

        rcyMyOrders.setAdapter(ordersAdapter);

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,-7);
        startdate.setText(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
        enddate.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        loadingProgressDialog = new LoadingProgressDialog(this);

    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @OnClick({R.id.startquery})
    public void query(View view) {
        getData();
    }
    @OnClick({R.id.startdate,R.id.enddate})
    public void onViewClicked(View view) {
        final Calendar calendar = Calendar.getInstance();

        DatePickerDialog dialog = new DatePickerDialog(MyOrdersActivity.this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {

                        calendar.set(Calendar.YEAR, year);
                        calendar.set(Calendar.MONTH, month);
                        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        if(view.getId() == R.id.startdate)
                            startdate.setText(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
                        else{
                            enddate.setText(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
                        }
                    }
                },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

    public void getData(){
        loadingProgressDialog.show();
        String userName = AppConfig.getString("userName");
        BaseRequest.getInstance().getApiServise().yuangxfjl(userName,startdate.getText() + " 00:00:00",enddate.getText() + " 23:59:59")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<List<OrderInfo>>(mContext) {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        loadingProgressDialog.hide();
                    }

                    @Override
                    public void onSuccess(BaseReponse<List<OrderInfo>> baseReponse) {
                        List<OrderInfo> result =  baseReponse.getResults();
                        k.clear();
                        k.addAll(result);
                        ordersAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCodeError(BaseReponse tBaseReponse) {
                        tBaseReponse.getCode();
                    }
                    @Override
                    public void onFailure(Throwable e, boolean netWork) throws Exception {
                        e.printStackTrace();
                    }
                });
    }
}
